package cn.otrue.patienthealthmanager.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import cn.otrue.patienthealthmanager.HealthApplication;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.http.HttpServer;

import com.google.gson.reflect.TypeToken;

/**
 * 日历控件 功能：获得点选的日期区间
 * 
 */
public class CalendarViewPlan extends View implements View.OnTouchListener {
	private final static String TAG = "anCalendar";
	private Date selectedStartDate;
	private Date selectedEndDate;
	private Date curDate = new Date(); // 当前日历显示的月
	private Date downDate; // 手指按下状态时临时日期
	private Date showFirstDate, showLastDate; // 日历显示的第一个日期和最后一个日期
	private int downIndex; // 按下的格子索引
	private Calendar calendar;
	private Surface surface;
	private int[] date = new int[42]; // 日历显示数字
	private int curStartIndex, curEndIndex; // 当前显示的日历起始的索引
	// private boolean completed = false; // 为false表示只选择了开始日期，true表示结束日期也选择了
	// 给控件设置监听事件
	private OnItemClickListener onItemClickListener;
	private boolean hasBeenPressed = false;
	private Date initDate;
	private Context ctx;

	public CalendarViewPlan(Context context) {
		super(context);
		ctx = context;
		init();
	}

	public CalendarViewPlan(Context context, AttributeSet attrs) {
		super(context, attrs);
		ctx = context;
		init();
	}

	public CalendarViewPlan(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		ctx = context;
		init();
	}

	public void setInitDate(Date date) {
		initDate = date;
		init();
	}

	private void init() {
		if (initDate != null)
			curDate = selectedStartDate = selectedEndDate = initDate;
		else
			curDate = selectedStartDate = selectedEndDate = new Date();
		calendar = Calendar.getInstance();
		calendar.setTime(curDate);
		surface = new Surface();
		surface.density = getResources().getDisplayMetrics().density;
		setBackgroundColor(surface.bgColor);
		setOnTouchListener(this);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		surface.width = getResources().getDisplayMetrics().widthPixels;
		surface.height = (int) (getResources().getDisplayMetrics().heightPixels * 2 / 5);
		// if (View.MeasureSpec.getMode(widthMeasureSpec) ==
		// View.MeasureSpec.EXACTLY) {
		// surface.width = View.MeasureSpec.getSize(widthMeasureSpec);
		// }
		// if (View.MeasureSpec.getMode(heightMeasureSpec) ==
		// View.MeasureSpec.EXACTLY) {
		// surface.height = View.MeasureSpec.getSize(heightMeasureSpec);
		// }
		widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(surface.width,
				View.MeasureSpec.EXACTLY);
		heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(surface.height,
				View.MeasureSpec.EXACTLY);
		setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		Log.d(TAG, "[onLayout] changed:"
				+ (changed ? "new size" : "not change") + " left:" + left
				+ " top:" + top + " right:" + right + " bottom:" + bottom);
		if (changed) {
			surface.init();
		}
		super.onLayout(changed, left, top, right, bottom);
	}

	private String bloodpressureId;
	private String bloodsugarId;
	private String drugplanId;
	private Calendar bloodsugar_begintime = Calendar.getInstance();;
	private Calendar bloodsugar_endtime = Calendar.getInstance();;
	private String bloodsugar_week;
	private Calendar bloodpressure_begintime = Calendar.getInstance();;
	private Calendar bloodpressure_endtime = Calendar.getInstance();;
	private String bloodpressure_week;
	private Calendar drugplan_begintime = Calendar.getInstance();;
	private Calendar drugplan_endtime = Calendar.getInstance();;
	private String drugplan_week;
	private Calendar newCalendar = Calendar.getInstance();
	private SimpleDateFormat formatBuilder = new SimpleDateFormat("yyyy-MM-dd");
	private Map<String, String> map;
	private String dateStr;

	@Override
	protected void onDraw(Canvas canvas) {
		Log.d(TAG, "onDraw");
		// 画框
		canvas.drawPath(surface.boxPath, surface.borderPaint);
		// 星期背景
		surface.cellBgPaint.setColor(Color.parseColor("#dff2fe"));
		canvas.drawRect(0, surface.weekHeight, surface.width,
				surface.weekHeight - surface.height, surface.cellBgPaint);
		// canvas.drawRect(surface.weekHeight, surface.width,surface.weekHeight,
		// surface.width, surface.cellBgPaint);
		// 星期
		float weekTextY = surface.monthHeight + surface.weekHeight * 3 / 4f;

		for (int i = 0; i < surface.weekText.length; i++) {
			float weekTextX = i
					* surface.cellWidth
					+ (surface.cellWidth - surface.weekPaint
							.measureText(surface.weekText[i])) / 2f;
			canvas.drawText(surface.weekText[i], weekTextX, weekTextY,
					surface.weekPaint);
		}
		// 计算日期
		calculateDate();
		// 给按下状态和处于选择状态的日期画背景色
		drawDownOrSelectedBg(canvas);
		// write date number
		// today index
		int todayIndex = -1;
		int dowmDayIndex = -1;
		calendar.setTime(curDate);
		String curYearAndMonth = calendar.get(Calendar.YEAR) + ""
				+ calendar.get(Calendar.MONTH);
		calendar.setTime(selectedStartDate);// today
		String todayYearAndMonth = calendar.get(Calendar.YEAR) + ""
				+ calendar.get(Calendar.MONTH);
		if (curYearAndMonth.equals(todayYearAndMonth)) {// 如果选择的月份与当前是同一月
			int todayNumber = calendar.get(Calendar.DAY_OF_MONTH);
			todayIndex = curStartIndex + todayNumber - 1;
			calendar.setTime(selectedStartDate);
			dowmDayIndex = curStartIndex + calendar.get(Calendar.DAY_OF_MONTH)
					- 1;

		}

		dateStr = getDateBySp();
		Log.e("calendar", dateStr);
		map = HttpServer.gson.fromJson(dateStr,
				new TypeToken<Map<String, String>>() {
				}.getType());

		if (!dateStr.equals("")) {
			try {

				if (map.containsKey("bloodsugarId")
						&& !map.get("bloodsugarId").isEmpty()) {
					bloodsugarId = map.get("bloodsugarId") + "";
					String uuu = map.get("bloodsugar_begintime");
					String iii = map.get("bloodsugar_endtime");
					bloodsugar_begintime.setTime(formatBuilder.parse(uuu));
					bloodsugar_endtime.setTime(formatBuilder.parse(iii));
					bloodsugar_week = map.get("bloodpressureweeks");
				}

				if (map.containsKey("bloodpressureId")
						&& !map.get("bloodpressureId").isEmpty()) {
					bloodpressureId = map.get("bloodpressureId") + "";

					String uuu = map.get("bloodpressure_begintime");
					String iii = map.get("bloodpressure_endtime");
					bloodpressure_begintime.setTime(formatBuilder.parse(uuu));
					bloodpressure_endtime.setTime(formatBuilder.parse(iii));
					bloodpressure_week = map.get("bloodpressure_week");
				}
				if (map.containsKey("drugplanId")
						&& !map.get("drugplanId").isEmpty()) {
					drugplanId = map.get("drugplanId") + "";
					String uuu = map.get("drugplan_begintime");
					String iii = map.get("drugplan_endtime");
					drugplan_begintime.setTime(formatBuilder.parse(uuu));
					drugplan_endtime.setTime(formatBuilder.parse(iii));
					drugplan_week = map.get("drugplan_week");
				}

			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		for (int i = 41; i >= 0; i--) {
			int color = surface.textColor;

			if (i == todayIndex && !hasBeenPressed) {
				color = surface.selectDateColor;
			} else if (i == downIndex && hasBeenPressed) {
				color = surface.selectDateColor;
			} else if (i == dowmDayIndex && hasBeenPressed) {
				color = surface.selectDateColor;
			}

			if (isLastMonth(i)) {
				color = surface.borderColor;
			} else if (isNextMonth(i)) {
				color = surface.borderColor;
			}
			drawCellText(canvas, i, date[i] + "", color);
		}

		super.onDraw(canvas);
	}

	private String getDateBySp() {
		SharedPreferences mySharedPreferences = HealthApplication.getInstance()
				.getSharedPreferences("calendar", Activity.MODE_PRIVATE);

		return mySharedPreferences.getString("plan", "");

	}

	/**
	 * 普通日期的绘制方法（可在此方法中改变指定日期的背景色）TODO
	 * 
	 * @param canvas
	 * @param index
	 * @param text
	 * @param copyHealthData
	 */
	private void drawCellText(Canvas canvas, int index, String text, int color) {
		int x = getXByIndex(index);
		int y = getYByIndex(index);
		surface.datePaint.setColor(color);
		float cellY = surface.monthHeight + surface.weekHeight + (y - 1)
				* surface.cellHeight + surface.cellHeight * 3 / 4f;
		float cellX = (surface.cellWidth * (x - 1))
				+ (surface.cellWidth - surface.datePaint.measureText(text))
				/ 2f;
		newCalendar.setTime(curDate);
		int m = newCalendar.get(Calendar.MONTH);
		int d = Integer.parseInt(text);
		if (index <= 14) {
			if (d > 15) {
				m = m - 1;
			}
		} else {
			if (d < 15) {
				m = m + 1;
			}
		}
		newCalendar.set(Calendar.MONTH, m);
		newCalendar.set(Calendar.DAY_OF_MONTH, d);
		// n比x早,返回-1,
		// n与x相同,返回0
		// n比x晚,返回1
		boolean suger = false;
		boolean pressure = false;
		boolean drug = false;
		if (!dateStr.equals("")) {
			if (map.containsKey("bloodsugarId")
					&& !map.get("bloodsugarId").isEmpty()) {
				if (newCalendar.compareTo(bloodsugar_begintime) != -1
						&& newCalendar.compareTo(bloodsugar_endtime) != 1) {
					String[] weekArr = bloodsugar_week.split(",");
					for (int i = 0; i < weekArr.length; i++) {
						if ((newCalendar.get(Calendar.DAY_OF_WEEK) - 1) == Integer
								.parseInt(weekArr[i])) {
							drug = true;
							break;
						}
					}
				}
			} else {

			}
			if (map.containsKey("bloodpressureId")
					&& !map.get("bloodpressureId").isEmpty()) {
				if (newCalendar.compareTo(bloodpressure_begintime) != -1
						&& newCalendar.compareTo(bloodpressure_endtime) != 1) {
					String[] weekArr = bloodpressure_week.split(",");
					for (int i = 0; i < weekArr.length; i++) {
						if ((newCalendar.get(Calendar.DAY_OF_WEEK) - 1) == Integer
								.parseInt(weekArr[i])) {
							pressure = true;
							break;
						}
					}
				}
			}
			if (map.containsKey("drugplanId")
					&& !map.get("drugplanId").isEmpty()) {
				if (newCalendar.compareTo(drugplan_begintime) != -1
						&& newCalendar.compareTo(drugplan_endtime) != 1) {
					String[] weekArr = drugplan_week.split(",");
					for (int i = 0; i < weekArr.length; i++) {
						if ((newCalendar.get(Calendar.DAY_OF_WEEK) - 1) == Integer
								.parseInt(weekArr[i])) {
							suger = true;
							break;
						}
					}
				}
			}
		}

		if (suger && pressure && drug) {
			drawCellBg(canvas, index, Color.parseColor("#afd0e3"), 7);
		} else if (suger && pressure && !drug) {
			drawCellBg(canvas, index, Color.parseColor("#afd0e3"), 6);
		} else if (suger && !pressure && drug) {
			drawCellBg(canvas, index, Color.parseColor("#afd0e3"), 5);
		} else if (!suger && pressure && drug) {
			drawCellBg(canvas, index, Color.parseColor("#afd0e3"), 4);
		} else if (suger && !pressure && !drug) {
			drawCellBg(canvas, index, Color.parseColor("#afd0e3"), 3);
		} else if (!suger && pressure && !drug) {
			drawCellBg(canvas, index, Color.parseColor("#afd0e3"), 2);
		} else if (!suger && !pressure && drug) {
			drawCellBg(canvas, index, Color.parseColor("#afd0e3"), 1);
		}

		canvas.drawText(text, cellX, cellY, surface.datePaint);
	}

	/**
	 * 
	 * @param canvas
	 * @param index
	 * @param color
	 */
	private void drawCellBg(Canvas canvas, int index, int color, int type) {
		int x = getXByIndex(index);
		int y = getYByIndex(index);
		surface.cellBgPaint.setColor(color);
		float left = surface.cellWidth * (x - 1) + surface.borderWidth;
		float top = surface.monthHeight + surface.weekHeight + (y - 1)
				* surface.cellHeight + surface.borderWidth;
		if (type == 0) {
			canvas.drawRect(left, top, left + surface.cellWidth
					- surface.borderWidth, top + surface.cellHeight
					- surface.borderWidth, surface.cellBgPaint);
		} else {
			Bitmap bmp = null;
			switch (type) {
			case 1:
				bmp = BitmapFactory.decodeResource(HealthApplication
						.getInstance().getResources(), R.drawable.calender1);
				break;
			case 2:
				bmp = BitmapFactory.decodeResource(HealthApplication
						.getInstance().getResources(), R.drawable.calender2);
				break;
			case 3:
				bmp = BitmapFactory.decodeResource(HealthApplication
						.getInstance().getResources(), R.drawable.calender3);
				break;
			case 4:
				bmp = BitmapFactory.decodeResource(HealthApplication
						.getInstance().getResources(), R.drawable.calender4);
				break;
			case 5:
				bmp = BitmapFactory.decodeResource(HealthApplication
						.getInstance().getResources(), R.drawable.calender5);
				break;
			case 6:
				bmp = BitmapFactory.decodeResource(HealthApplication
						.getInstance().getResources(), R.drawable.calender6);
				break;
			case 7:
				bmp = BitmapFactory.decodeResource(HealthApplication
						.getInstance().getResources(), R.drawable.calender7);
				break;
			default:

			}

			canvas.drawBitmap(bmp, left, top, surface.cellBgPaint);
		}

	}

	private void calculateDate() {
		calendar.setTime(curDate);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		int dayInWeek = calendar.get(Calendar.DAY_OF_WEEK);
		Log.d(TAG, "day in week:" + dayInWeek);
		int monthStart = dayInWeek;
		if (monthStart == 1) {
			monthStart = 8;
		}
		monthStart -= 1; // 以日为开头-1，以星期一为开头-2
		curStartIndex = monthStart;
		date[monthStart] = 1;
		// last month
		if (monthStart > 0) {
			calendar.set(Calendar.DAY_OF_MONTH, 0);
			int dayInmonth = calendar.get(Calendar.DAY_OF_MONTH);
			for (int i = monthStart - 1; i >= 0; i--) {
				date[i] = dayInmonth;
				dayInmonth--;
			}
			calendar.set(Calendar.DAY_OF_MONTH, date[0]);
		}
		showFirstDate = calendar.getTime();
		// this month
		calendar.setTime(curDate);
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 0);
		int monthDay = calendar.get(Calendar.DAY_OF_MONTH);
		for (int i = 1; i < monthDay; i++) {
			date[monthStart + i] = i + 1;
		}
		curEndIndex = monthStart + monthDay;
		// next month
		for (int i = monthStart + monthDay; i < 42; i++) {
			date[i] = i - (monthStart + monthDay) + 1;
		}
		if (curEndIndex < 42) {
			// 显示了下一月的
			calendar.add(Calendar.DAY_OF_MONTH, 1);
		}
		calendar.set(Calendar.DAY_OF_MONTH, date[41]);
		showLastDate = calendar.getTime();
	}

	private void drawDownOrSelectedBg(Canvas canvas) {
		// down and not up
		if (downDate != null) {
			drawCellBg(canvas, downIndex, surface.cellDownColor, 0);
		}
		// selected bg color
		if (!selectedEndDate.before(showFirstDate)
				&& !selectedStartDate.after(showLastDate)) {
			int[] section = new int[] { -1, -1 };
			calendar.setTime(curDate);
			calendar.add(Calendar.MONTH, -1);
			findSelectedIndex(0, curStartIndex, calendar, section);
			if (section[1] == -1) {
				calendar.setTime(curDate);
				findSelectedIndex(curStartIndex, curEndIndex, calendar, section);
			}
			if (section[1] == -1) {
				calendar.setTime(curDate);
				calendar.add(Calendar.MONTH, 1);
				findSelectedIndex(curEndIndex, 42, calendar, section);
			}
			if (section[0] == -1) {
				section[0] = 0;
			}
			if (section[1] == -1) {
				section[1] = 41;
			}
			for (int i = section[0]; i <= section[1]; i++) {
				drawCellBg(canvas, i, surface.cellSelectedColor, 0);
			}
		}
	}

	private void findSelectedIndex(int startIndex, int endIndex,
			Calendar calendar, int[] section) {
		for (int i = startIndex; i < endIndex; i++) {
			calendar.set(Calendar.DAY_OF_MONTH, date[i]);
			Date temp = calendar.getTime();
			// Log.d(TAG, "temp:" + temp.toLocaleString());
			if (temp.compareTo(selectedStartDate) == 0) {
				section[0] = i;
			}
			if (temp.compareTo(selectedEndDate) == 0) {
				section[1] = i;
				return;
			}
		}
	}

	public Date getSelectedStartDate() {
		return selectedStartDate;
	}

	public Date getSelectedEndDate() {
		return selectedEndDate;
	}

	private boolean isLastMonth(int i) {
		if (i < curStartIndex) {
			return true;
		}
		return false;
	}

	private boolean isNextMonth(int i) {
		if (i >= curEndIndex) {
			return true;
		}
		return false;
	}

	private int getXByIndex(int i) {
		return i % 7 + 1; // 1 2 3 4 5 6 7
	}

	private int getYByIndex(int i) {
		return i / 7 + 1; // 1 2 3 4 5 6
	}

	// 获得当前应该显示的年月
	public String getYearAndmonth() {
		calendar.setTime(curDate);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		return year + "年" + surface.monthText[month] + "月";
	}

	// 上一月
	public String clickLeftMonth() {
		calendar.setTime(curDate);
		calendar.add(Calendar.MONTH, -1);
		curDate = calendar.getTime();
		downIndex = -1;
		invalidate();
		return getYearAndmonth();
	}

	// 下一月
	public String clickRightMonth() {
		calendar.setTime(curDate);
		calendar.add(Calendar.MONTH, 1);
		curDate = calendar.getTime();
		downIndex = -1;
		invalidate();
		return getYearAndmonth();
	}

	private void setSelectedDateByCoor(float x, float y) {
		// cell click down
		if (y > surface.monthHeight + surface.weekHeight) {
			int m = (int) (Math.floor(x / surface.cellWidth) + 1);
			int n = (int) (Math
					.floor((y - (surface.monthHeight + surface.weekHeight))
							/ Float.valueOf(surface.cellHeight)) + 1);
			downIndex = (n - 1) * 7 + m - 1;
			Log.d(TAG, "downIndex:" + downIndex);
			calendar.setTime(curDate);

			if (isLastMonth(downIndex)) {
				calendar.add(Calendar.MONTH, -1);

				calendar.set(Calendar.DAY_OF_MONTH, date[downIndex]);

			} else if (isNextMonth(downIndex)) {
				calendar.add(Calendar.MONTH, 1);

				calendar.set(Calendar.DAY_OF_MONTH, date[downIndex]);

			}
			calendar.set(Calendar.DAY_OF_MONTH, date[downIndex]);
			downDate = calendar.getTime();
		}
		hasBeenPressed = true;// 设置点击标记为真
		// invalidate();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setSelectedDateByCoor(event.getX(), event.getY());
			break;
		case MotionEvent.ACTION_UP:
			if (downDate != null) {
				// if (!completed) {
				// if (downDate.before(selectedStartDate)) {
				// selectedEndDate = selectedStartDate;
				// selectedStartDate = downDate;
				// } else {
				// selectedEndDate = downDate;
				// }
				// completed = true;
				// } else {
				// selectedStartDate = selectedEndDate = downDate;
				// completed = false;
				// }
				if (downDate.after(new Date())) {
					return true;
				}
				selectedEndDate = selectedStartDate = curDate = downDate;
				// 响应监听事件
				onItemClickListener.OnItemClick(selectedStartDate);
				// Log.d(TAG, "downdate:" + downDate.toLocaleString());
				// Log.d(TAG, "start:" + selectedStartDate.toLocaleString());
				// Log.d(TAG, "end:" + selectedEndDate.toLocaleString());
				downDate = null;
				invalidate();

			}

			break;
		}
		return true;
	}

	// 给控件设置监听事件
	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		this.onItemClickListener = onItemClickListener;
	}

	// 监听接口
	public interface OnItemClickListener {
		void OnItemClick(Date date);
	}

	/**
	 * 
	 * 1. 布局尺寸 2. 文字颜色，大小 3. 当前日期的颜色，选择的日期颜色
	 */
	private class Surface {
		public float density;
		public int width; // 整个控件的宽度
		public int height; // 整个控件的高度
		public float monthHeight; // 显示月的高度
		// public float monthChangeWidth; // 上一月、下一月按钮宽度
		public float weekHeight; // 显示星期的高度
		public float cellWidth; // 日期方框宽度
		public float cellHeight; // 日期方框高度
		public float borderWidth;
		public int bgColor = Color.parseColor("#FFFFFF");
		private int textColor = Color.BLACK;
		// private int textColorUnimportant = Color.parseColor("#666666");
		// private int btnColor = Color.parseColor("#666666");
		private int borderColor = Color.parseColor("#CCCCCC");
		public int selectDateColor = Color.WHITE;
		public int cellDownColor = Color.parseColor("#CCFFFF");
		public int cellSelectedColor = Color.parseColor("#1f3c57");// Color.parseColor("#99CCFF");
		public Paint borderPaint;
		public Paint monthPaint;
		public Paint weekPaint;
		public Paint datePaint;
		// public Paint monthChangeBtnPaint;
		public Paint cellBgPaint;
		public Path boxPath; // 边框路径

		public String[] weekText = { "日", "一", "二", "三", "四", "五", "六" };
		public String[] monthText = { "01", "02", "03", "04", "05", "06", "07",
				"08", "09", "10", "11", "12" };

		public void init() {
			float temp = height / 7f;
			monthHeight = 0;// (float) ((temp + temp * 0.3f) * 0.6);
			// monthChangeWidth = monthHeight * 1.5f;
			weekHeight = (float) ((temp + temp * 0.3f) * 0.7);
			cellHeight = (height - monthHeight - weekHeight) / 6f;// cellHeight
																	// = (height
																	// -
																	// monthHeight
																	// -
																	// weekHeight)
																	// / 6f;
			cellWidth = width / 7f;
			borderPaint = new Paint();
			borderPaint.setColor(borderColor);
			borderPaint.setStyle(Paint.Style.STROKE);
			borderWidth = (float) (0.5 * density);
			borderWidth = borderWidth < 1 ? 1 : borderWidth;
			borderPaint.setStrokeWidth(borderWidth);
			monthPaint = new Paint();
			monthPaint.setColor(textColor);
			monthPaint.setAntiAlias(true);
			float textSize = cellHeight * 0.5f;// float textSize = cellHeight *
												// 0.4f;
			Log.d(TAG, "text size:" + textSize);
			monthPaint.setTextSize(textSize);
			monthPaint.setTypeface(Typeface.DEFAULT_BOLD);
			weekPaint = new Paint();
			weekPaint.setColor(textColor);
			weekPaint.setAntiAlias(true);
			float weekTextSize = weekHeight * 0.6f;
			weekPaint.setTextSize(weekTextSize);
			weekPaint.setTypeface(Typeface.DEFAULT_BOLD);
			datePaint = new Paint();
			datePaint.setColor(textColor);
			datePaint.setAntiAlias(true);
			float cellTextSize = cellHeight * 0.65f;// float cellTextSize =
													// cellHeight * 0.5f;
			datePaint.setTextSize(cellTextSize);
			datePaint.setTypeface(Typeface.DEFAULT);
			boxPath = new Path();
			boxPath.addRect(0, 0, width, height, Direction.CW);
			boxPath.moveTo(0, monthHeight);
			boxPath.rLineTo(width, 0);
			boxPath.moveTo(0, monthHeight + weekHeight);
			boxPath.rLineTo(width, 0);
			for (int i = 1; i < 6; i++) {
				boxPath.moveTo(0, monthHeight + weekHeight + i * cellHeight);
				boxPath.rLineTo(width, 0);
				boxPath.moveTo(i * cellWidth, monthHeight);
				boxPath.rLineTo(0, height - monthHeight);
			}
			boxPath.moveTo(6 * cellWidth, monthHeight);
			boxPath.rLineTo(0, height - monthHeight);
			cellBgPaint = new Paint();
			cellBgPaint.setAntiAlias(true);
			cellBgPaint.setStyle(Paint.Style.FILL);
			cellBgPaint.setColor(cellSelectedColor);
		}
	}

}
